package com.asia.order.utils;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;

public class ThreeDes {

	public static final String ALGORITHM = "DESede/ECB/NoPadding"; 
	// 加密算法/模式/填充,算法可用
	// DES,DESede,Blowfish
	public static byte keyBytes[];

	public static byte vector[] = null;

	static {
		try {

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static byte[] encryptMode( byte[] keybyte, byte[] src) {
		try {
			Cipher cipher = Cipher.getInstance(ALGORITHM);
			DESedeKeySpec desKeySpec = new DESedeKeySpec(keybyte);
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
			SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
			cipher.init(Cipher.ENCRYPT_MODE, secretKey);
			return cipher.doFinal(src);
		} catch (java.security.NoSuchAlgorithmException e1) {
			e1.printStackTrace();
		} catch (javax.crypto.NoSuchPaddingException e2) {
			e2.printStackTrace();
		} catch (java.lang.Exception e3) {
			e3.printStackTrace();
		}
		return null;
	}

	// keybyte为加密密钥，长度为24字节
	// src为加密后的缓冲区
	public static byte[] decryptMode( byte[] keybyte,
			byte[] src) {
		try {
			// 生成密钥
			Cipher cipher = Cipher.getInstance(ALGORITHM);
			DESedeKeySpec desKeySpec = new DESedeKeySpec(keybyte);
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
			SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
			cipher.init(Cipher.DECRYPT_MODE, secretKey);
			return cipher.doFinal(src);
		} catch (java.security.NoSuchAlgorithmException e1) {
			e1.printStackTrace();
		} catch (javax.crypto.NoSuchPaddingException e2) {
			e2.printStackTrace();
		} catch (java.lang.Exception e3) {
			e3.printStackTrace();
		}
		return null;
	}

	public static String getKeyBytes() {
		java.util.Random ran = new java.util.Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < 3; i++) {
			sb.append(Integer.toString(ran.nextInt(100000000)));
		}
		while(true){
			if(sb.length()==24){
				break;
			}else if(sb.length() < 24){
				sb.append('0');
			}else{
				sb.deleteCharAt(sb.length()-1);
			}
		}
		return sb.toString();
	}

	public static void main(String[] args) throws Exception {
		// 添加新安全算法,如果用JCE就要把它添加进去
		//keyBytes = getKeyBytes().getBytes();
		keyBytes = "38323335373632323832333537333230".getBytes(); //012345678901234567890123
		System.out.println("keyBytes:==="+new String(keyBytes));
		String szSrc = "7DBBE00B69D57D4E"; 
		System.out.println("加密前的字符串:" + szSrc);
		byte[] encoded = encryptMode( keyBytes, szSrc.getBytes());
		for(byte b : encoded){
			System.out.println(b);
		}
		System.out.println("加密后的字符串:" + new String(encoded)); 
		//byte[] s = new String(encoded,"utf-8").getBytes();
		//for(byte b : s){
		//	System.out.println(b);
		//}
		//System.out.println("字符串转化成byte数组后转成字符串："+new String(s));
//		byte[] srcBytes = decryptMode(keyBytes, new String(com.linkage.bss.crm.util.facade.Hex.decodeHex("c15a0022448ac6cc".toCharArray()),"iso-8859-1").getBytes("iso-8859-1"));// "pwdStr":"撠]?9\u000e|","keyBytes":"105923339113006682967920"
		//byte[] srcBytes = decryptMode(keyBytes,new String(encoded,"iso-8859-1").getBytes("iso-8859-1")); 
//		System.out.println("解密后的字符串:" + (new String(srcBytes)));
	}
}